package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entity.CommonResult;
import com.atguigu.springcloud.entity.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/id/{id}")
    public CommonResult<Payment> queryById(@PathVariable("id") Long id){
        Payment byId = paymentService.getById(id);
        if (byId!=null) {
            return new CommonResult<>(200,"success:"+serverPort,byId);
        }else{
            return new CommonResult<>(404,"query null");
        }
    }

    @GetMapping(value = "/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
        Payment payment = paymentService.getById(id);
        log.info("*****查询结果:{}",payment);
        if (payment != null) {
            return new CommonResult<>(200,"查询成功",payment);
        }else{
            return new CommonResult<>(444,"没有对应记录,查询ID: "+id,null);
        }
    }

    @PostMapping
    public CommonResult<Integer> save(@RequestBody Payment payment){
        int i = paymentService.create(payment);
        if (i>0) {
            return new CommonResult<>(200,"success:"+serverPort,payment.getId().intValue());
        }else{
            return new CommonResult<>(444,"failed");
        }
    }

    @GetMapping(value = "/lb")
    public String getPaymentLB()
    {
        return serverPort;
    }


}
